/*
 ----------------------------------------------------------------------------------
                          "THE BEER-WARE COPYRIGHT LICENSE"
                                    (Revision 43)
                                     -----------
   Rymek [rymek@rymek.org] wrote this file. As long as You retain this notice and
   this file has been sent directly to You by the author, You can do whatever You
   want with this stuff as long as You don't sell or rent it. If we meet some day,
   and You think this stuff is worth it, You can buy me a beer in return :-)
                                                                __________
                                                                Paweł Rymaszewski
 ----------------------------------------------------------------------------------
 */

package utils;

import constants.Constants;
import constants.PropertyConstants;
import processing.objects.Result;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;


/**
 * Class for exporting various config/result information
 * to a flat models/xml file
 *
 * @author rymek
 *         User: rymek
 *         Date: 30.06.11
 *         Time: 10:10
 */
public class FileUtils {

    /**
     * Exports input string into specific dir, into specific file
     *
     * @param dir      directory name
     * @param fileName file name
     * @param content  file contents
     * @throws IOException thrown when I/O error occurs
     */
    public static void exportContent(String dir, String fileName, String content)
            throws IOException {

        Path tempDir = Paths.get(System.getProperty(PropertyConstants.PROP_USER_DIR), Constants.TEMP_DIR);

        if (!Files.exists(Paths.get(System.getProperty(PropertyConstants.PROP_USER_DIR), Constants.TEMP_DIR))) {

            tempDir = Files.createDirectories(Paths.get(System.getProperty(PropertyConstants.PROP_USER_DIR), Constants.TEMP_DIR));
            tempDir.toFile().deleteOnExit();

        } else tempDir.toFile().deleteOnExit();

        Path tempResultDir = Paths.get(tempDir.toString(), dir);

        if (!Files.exists(Paths.get(tempDir.toString(), dir))) {

            tempResultDir = Files.createDirectories(Paths.get(tempDir.toString(), dir));
            tempResultDir.toFile().deleteOnExit();

        } else tempResultDir.toFile().deleteOnExit();

        Path tempResultFile = Paths.get(tempResultDir.toString(), fileName);

        if (!Files.exists(Paths.get(tempResultDir.toString(), fileName))) {

            tempResultFile = Files.createFile(Paths.get(tempResultDir.toString(), fileName));
            Files.write(tempResultFile, content.getBytes());
            tempResultFile.toFile().deleteOnExit();

        } else tempResultFile.toFile().deleteOnExit();
    }

    /**
     * Copies output XML file to a file specified by user
     *
     * @param result          source for copy
     * @param destinationFile destination file
     * @throws IOException thrown when file cannot be copied
     */
    public static void copyFile(Result result, File destinationFile) throws IOException {

        Path source = Paths.get(result.getXmlResultFile().toURI());
        Path destination = Paths.get(destinationFile.toURI());

        Files.copy(source, destination);
    }

    /**
     * Merges Request and Response to specified file
     *
     * @param destinationFile destination file
     * @param content         content to export
     * @throws IOException thrown when IO error occurs
     */
    public static void mergeMessagesToFile(File destinationFile, String content) throws IOException {

        Path destination = Paths.get(destinationFile.toURI());
        Files.write(destination, content.getBytes());
    }
}
